<template>
  <div class="dashboard-container">
    <!-- 统计卡片 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card">
          <div class="card-header">
            <div class="card-icon book-icon">
              <el-icon><i-ep-reading /></el-icon>
            </div>
            <div class="card-data">
              <div class="card-title">图书总数</div>
             
              <div class="card-value">{{ statisticsData.totalBooks }}</div>
            </div>
          </div>
          <div class="card-footer">
            <span>较上月</span>
            <span class="up">
              <el-icon><i-ep-top /></el-icon>
              {{ statisticsData.bookGrowth }}%
            </span>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="box-card">
          <div class="card-header">
            <div class="card-icon order-icon">
              <el-icon><i-ep-shopping-cart /></el-icon>
            </div>
            <div class="card-data">
              <div class="card-title">订单总量</div>
              <div class="card-value">{{ statisticsData.totalOrders }}</div>
            </div>
          </div>
          <div class="card-footer">
            <span>较上月</span>
            <span class="up">
              <el-icon><i-ep-top /></el-icon>
              {{ statisticsData.orderGrowth }}%
            </span>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="box-card">
          <div class="card-header">
            <div class="card-icon user-icon">
              <el-icon><i-ep-user /></el-icon>
            </div>
            <div class="card-data">
              <div class="card-title">用户总数</div>
              <div class="card-value">{{ statisticsData.totalUsers }}</div>
            </div>
          </div>
          <div class="card-footer">
            <span>较上月</span>
            <span class="up">
              <el-icon><i-ep-top /></el-icon>
              {{ statisticsData.userGrowth }}%
            </span>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="box-card">
          <div class="card-header">
            <div class="card-icon revenue-icon">
              <el-icon><i-ep-money /></el-icon>
            </div>
            <div class="card-data">
              <div class="card-title">销售额</div>
              <div class="card-value">¥{{ statisticsData.totalRevenue }}</div>
            </div>
          </div>
          <div class="card-footer">
            <span>较上月</span>
            <span class="up">
              <el-icon><i-ep-top /></el-icon>
              {{ statisticsData.revenueGrowth }}%
            </span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    

    
    <!-- 最近活动与热门图书 -->
    <el-row :gutter="20" class="activity-row">
      <el-col :span="12">
        <el-card class="box-card">
          <template #header>
            <div class="card-title">最近活动</div>
          </template>
          <div class="activity-list">
            <div v-for="(activity, index) in recentActivities" :key="index" class="activity-item">
              <div class="activity-time">{{ activity.time }}</div>
              <div class="activity-content">
                <div class="activity-title">{{ activity.title }}</div>
                <div class="activity-desc">{{ activity.content }}</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card class="box-card">
          <template #header>
            <div class="card-title">热门图书</div>
          </template>
          <el-table :data="hotBooks" style="width: 100%" max-height="400">
            <el-table-column prop="title" label="图书名称" />
            <el-table-column prop="author" label="作者" width="120" />
            <el-table-column prop="sales" label="销量" width="100" align="center" />
            <el-table-column label="操作" width="100" align="center">
              <template #default="scope">
                <el-button link type="primary" @click="viewBookDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { dashboardAPI } from '../../utils/api'
import { ElLoading } from 'element-plus'

const router = useRouter()
const salesType = ref('month')
const loading = ref(false)

// 统计数据默认值
const statisticsData = reactive({
  totalBooks: 0,
  totalOrders: 0,
  totalUsers: 0,
  totalRevenue: '0.00',
  bookGrowth: 0,
  orderGrowth: 0,
  userGrowth: 0,
  revenueGrowth: 0
})

// 最近活动
const recentActivities = ref([])

// 热门图书
const hotBooks = ref([])

// 获取仪表盘统计数据
const fetchStatistics = async () => {
  try {
    const result = await dashboardAPI.getStatistics()
    
    if (result.code === 200) {
      // 将API返回数据合并到统计数据中
      Object.assign(statisticsData, result.data)
      
      // 格式化总收入，添加千位分隔符
      if (statisticsData.totalRevenue) {
        statisticsData.totalRevenue = Number(statisticsData.totalRevenue).toLocaleString('zh-CN', {
          minimumFractionDigits: 2,
          maximumFractionDigits: 2
        })
      }
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

// 获取热门图书数据
const fetchHotBooks = async () => {
  try {
    const result = await dashboardAPI.getHotBooks()
    
    if (result.code === 200) {
      hotBooks.value = result.data
    }
  } catch (error) {
    console.error('获取热门图书数据失败:', error)
  }
}

// 获取最近活动数据
const fetchRecentActivities = async () => {
  try {
    const result = await dashboardAPI.getRecentActivities()
    
    if (result.code === 200) {
      recentActivities.value = result.data
    }
  } catch (error) {
    console.error('获取最近活动数据失败:', error)
  }
}

// 查看图书详情
const viewBookDetail = (book) => {
  router.push(`/books?id=${book.id}`)
}

// 加载所有数据
const loadAllData = async () => {
  const loadingInstance = ElLoading.service({
    target: '.dashboard-container',
    text: '加载中...'
  })
  
  try {
    await Promise.all([
      fetchStatistics(),
      fetchHotBooks(),
      fetchRecentActivities()
    ])
  } catch (error) {
    console.error('加载数据失败:', error)
  } finally {
    loadingInstance.close()
  }
}

// 页面加载
onMounted(() => {
  loadAllData()
})
</script>

<style scoped>
.dashboard-container {
  height: 100%;
  width: 100%;
  padding: 10px;
  overflow-y: auto;
}

.box-card {
  margin-bottom: 10px;
}

.card-header {
  display: flex;
  align-items: center;
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 30px;
  color: #fff;
}

.book-icon {
  background-color: #1890ff;
}

.order-icon {
  background-color: #52c41a;
}

.user-icon {
  background-color: #fa8c16;
}

.revenue-icon {
  background-color: #f5222d;
}

.card-data {
  flex-grow: 1;
}

.card-title {
  font-size: 16px;
  color: #606266;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.card-footer {
  margin-top: 15px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #909399;
}

.up {
  color: #67c23a;
  margin-left: 8px;
  display: flex;
  align-items: center;
}

.down {
  color: #f56c6c;
  margin-left: 8px;
  display: flex;
  align-items: center;
}

.chart-row {
  margin: 10px 0;
}

.activity-row {
  margin: 10px 0;
}

.chart-card {
  height: 350px;
}

.chart-container {
  height: 280px;
}

.placeholder-chart {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8f8f8;
  border-radius: 4px;
  color: #909399;
}

.activity-list {
  max-height: 350px;
  overflow-y: auto;
}

.activity-item {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px solid #ebeef5;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-time {
  width: 120px;
  color: #909399;
  font-size: 14px;
}

.activity-content {
  flex: 1;
}

.activity-title {
  font-weight: bold;
  margin-bottom: 5px;
}

.activity-desc {
  color: #606266;
  font-size: 14px;
}
</style> 